package com.sk.sourcecircle;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Window;
import android.widget.Toast;

import com.sina.weibo.sdk.api.WebpageObject;
import com.sina.weibo.sdk.api.WeiboMessage;
import com.sina.weibo.sdk.api.share.IWeiboHandler;
import com.sina.weibo.sdk.api.share.SendMessageToWeiboRequest;
import com.sina.weibo.sdk.auth.WeiboAuthListener;
import com.sina.weibo.sdk.constant.WBConstants;
import com.sina.weibo.sdk.exception.WeiboException;
import com.sina.weibo.sdk.utils.Utility;
import com.sk.sourcecircle.app.MyApplication;
import com.sk.sourcecircle.utils.ImageUtil;
import com.sk.sourcecircle.utils.MyUtils;

import java.io.IOException;

/**
 * Created by sk on 2018/1/14.
 */

public class WBShareActivity extends Activity implements IWeiboHandler.Response, WeiboAuthListener {

    private String title;
    private String imageUrl;
    private String targetUrl;
    private String summary;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_weibo_share);
        title = getIntent().getStringExtra("title");
        targetUrl = getIntent().getStringExtra("targetUrl");
        imageUrl = getIntent().getStringExtra("imageUrl");
        summary = getIntent().getStringExtra("summary");
        weiboShare();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        MyApplication.getInstance().getWeibo().handleWeiboResponse(intent, this);
    }

    /**
     * 分享到微博
     */
    private void weiboShare() {

        final WeiboMessage weiboMessage = new WeiboMessage();
        final WebpageObject mediaObject = new WebpageObject();
        mediaObject.identify = Utility.generateGUID();
        if (title != null && title.length() > 0) {
            mediaObject.title = title;
        } else {
            mediaObject.title = "源圈";
        }
        if (summary != null && summary.length() > 0) {
            mediaObject.description = summary;
        } else {
            mediaObject.description = "源圈";
        }

        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Bitmap thumb2;
                    if (imageUrl != null && imageUrl.length() > 0) {
                        Bitmap bitmap = MyUtils.getBitmap(imageUrl);
                        thumb2 = ImageUtil.imageZoom(bitmap);
                    } else {
                        thumb2 = BitmapFactory.decodeResource(getResources(), R.mipmap.logo);
                    }
                    mediaObject.setThumbImage(thumb2);
                    mediaObject.actionUrl = targetUrl;
                    weiboMessage.mediaObject = mediaObject;
                    // 2. 初始化从第三方到微博的消息请求
                    SendMessageToWeiboRequest request = new SendMessageToWeiboRequest();
                    // 用transaction唯一标识一个请求
                    request.transaction = String.valueOf(System.currentTimeMillis());
                    request.message = weiboMessage;

                    // 3. 发送请求消息到微博，唤起微博分享界面
                    MyApplication.getInstance().getWeibo().sendRequest(WBShareActivity.this, request);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        thread.start();
    }


    @Override
    public void onComplete(Bundle bundle) {

    }

    @Override
    public void onWeiboException(WeiboException e) {

    }

    @Override
    public void onCancel() {

    }


    //调用onResume,将这个空的Activity关闭掉
    @Override
    protected void onResume() {
        super.onResume();
        Boolean resp = MyApplication.getInstance().getWeibo().handleWeiboResponse(getIntent(), this);
    }


    @Override
    public void onResponse(com.sina.weibo.sdk.api.share.BaseResponse baseResponse) {
        if (baseResponse != null) {
            switch (baseResponse.errCode) {
                case WBConstants.ErrorCode.ERR_OK:
                    Toast.makeText(WBShareActivity.this, "分享成功！", Toast.LENGTH_SHORT).show();
                    break;
                case WBConstants.ErrorCode.ERR_CANCEL:
                    Toast.makeText(WBShareActivity.this, "分享取消！", Toast.LENGTH_SHORT).show();

                    break;
                case WBConstants.ErrorCode.ERR_FAIL:
                    Toast.makeText(WBShareActivity.this, "分享失败！", Toast.LENGTH_SHORT).show();
                    break;
            }

            finish();
        }
    }
}
